var util = require('../../../utils/util.js');
var api = require('../../../config/api.js');
var user = require('../../../utils/user.js');
var app = getApp();

Page({
  data: {
    userInfo: {
      nickName: '点击登录',
      avatarUrl: 'http://yanxuan.nosdn.127.net/8945ae63d940cc42406c3f67019c5cb6.png',
      mobile: ''
    },
    hasLogin: app.globalData.hasLogin
  },
  onLoad: function(options) {
    // 页面初始化 options为页面跳转所带来的参数
  },
  onReady: function() {

  },
  onShow: function() {
    this.setData({
      hasLogin: app.globalData.hasLogin
    })
    //获取用户的登录信息
    if (app.globalData.hasLogin) {
      let userInfo = wx.getStorageSync('userInfo');
      this.setData({
        userInfo: userInfo
      });
    }

  },
  onHide: function() {
    // 页面隐藏

  },
  onUnload: function() {
    // 页面关闭
  },
  goLogin() {
    if (!app.globalData.hasLogin) {
      wx.navigateTo({
        url: "/pages/auth/login/login"
      });
    }
  },

  cleanCache(){
    wx.showModal({
      title: '提示',
      content: '此操作会清空购物车，请慎重！',
      success: function (res) {
        if (res.confirm) {
          wx.setStorageSync("cart", [])
          wx.setStorageSync("cartOne", [])
          wx.setStorageSync("cartfalg", [])
          wx.showToast({
            title: '缓存已清除',
          })
        } 
      }
    })  
  },

  getTicket() {
    if (app.globalData.hasLogin) {
      wx.navigateTo({
        url: "/pages/ucenter/voucher/voucher"
      });
    } else {
      wx.navigateTo({
        url: "/pages/auth/login/login"
      });
    }
  },
  goWallet() {
    if (app.globalData.hasLogin) {
      wx.navigateTo({
        url: "/pages/ucenter/wallet/wallet"
      });
    } else {
      wx.navigateTo({
        url: "/pages/auth/login/login"
      });
    }
  },
  goOrder() {
    if (app.globalData.hasLogin) {
      wx.navigateTo({
        url: "/pages/ucenter/order/order"
      });
    } else {
      wx.navigateTo({
        url: "/pages/auth/login/login"
      });
    }
  },
  goCoupon() {
    if (app.globalData.hasLogin) {
      wx.navigateTo({
        url: "/pages/ucenter/coupon/coupon"
      });
    } else {
      wx.navigateTo({
        url: "/pages/auth/login/login"
      });
    };

  },
  goCollect() {
    if (app.globalData.hasLogin) {
      wx.navigateTo({
        url: "/pages/ucenter/collect/collect"
      });
    } else {
      wx.navigateTo({
        url: "/pages/auth/login/login"
      });
    };
  },
  goFootprint() {
    if (app.globalData.hasLogin) {
      wx.navigateTo({
        url: "/pages/ucenter/footprint/footprint"
      });
    } else {
      wx.navigateTo({
        url: "/pages/auth/login/login"
      });
    };
  },
  goAddress() {
    if (app.globalData.hasLogin) {
      wx.navigateTo({
        url: "/pages/ucenter/address/address"
      });
    } else {
      wx.navigateTo({
        url: "/pages/auth/login/login"
      });
    };
  },
  aboutUs: function() {
    wx.navigateTo({
      url: '/pages/about/about'
    });
  },
  exitLogin: function() {
    let that = this;
    wx.showModal({
      title: '',
      confirmColor: '#b4282d',
      content: '退出登录？',
      success: function(res) {
        if (res.confirm) {
          app.globalData.hasLogin = false;
          wx.removeStorageSync('token');
          wx.removeStorageSync('userInfo');
          wx.removeStorageSync("memberType");
          that.setData({
            userInfo: {
              nickName: '点击登录',
              avatarUrl: 'http://yanxuan.nosdn.127.net/8945ae63d940cc42406c3f67019c5cb6.png'
            }
          })
          wx.switchTab({
            url: '/pages/index/index'
          });
        }
      }
    })
  },
  getBindMobile: function() {
    let that = this;
    if (app.globalData.hasLogin) {
      if (that.data.userInfo.mobile != '') {
        wx.navigateTo({
          url: "/pages/auth/bindNewMobile/bindNewMobile"
        });
      }else{
        wx.navigateTo({
          url: "/pages/auth/bindMobile/bindMobile"
        });
      }
    } else {
      wx.navigateTo({
        url: "/pages/auth/login/login"
      });
    }
  },
  getPhoneNumber: function(e) {
    // console.log(e)
    // console.log(e.detail.errMsg)
    // console.log(e.detail.iv)
    // console.log(e.detail.encryptedData)
  }
})